<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform @deleteItem="deleteUserItem" ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {buildSubFormData, subOnLoad, changeEditor,configSelectData,hasValue} from '@/static/js/formUtil.js';
	import { ignoreHtml,ptFloat,minusF,getDateStr} from '@/static/js/common.js';
	import urlUtil from '@/static/js/urlUtil.js'
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				customerCacheKey:'mar-kh-visitPlan-title2-customer-cache',
				typeCacheKey:"mar-kh-visiPlan-title2-type-cache",
				peopleCacheKey:"mar-kh-visiPlan-title2-people-cache",
				visitUserCacheKey:'mar-kh-visiPlan-title2-visitUser-cache',
				visitTargetIdStrCacheKey:"mar-kh-visiPlan-title2-visitTargetIdStr-cache",
				visitPreparationIdStrCacheKey:"mar-kh-visiPlan-title2-visitPreparationIdStr-cache",
				exchangePointIdStrCacheKey:"mar-kh-visiPlan-title2-exchangePointIdStr-cache",
				customerOwnerId: '',
				customerOwner: ''
			})
		},
		methods:{
			clearCustomer(){
				let _that = this;
				_that.$refs.autoform.clear('title2', 'customerNameId');
				_that.$refs.autoform.clear('title2', 'customerId');
				_that.$refs.autoform.clear('title2', 'customerName');
				_that.$refs.autoform.clear('title2', 'customerShortName');
				_that.$refs.autoform.clear('title2', 'problemRemark');
				_that.$refs.autoform.clear('title2', 'transactiontatusId');
				_that.$refs.autoform.clear('title2', 'developmentDate');
				_that.$refs.autoform.clear('title2', 'orderDate');
				_that.$refs.autoform.clear('title2', 'visitUserId');
			},
			onSelectContactType(item){
				let _that = this;
				if(item.code == '39425'){
					_that.$refs.autoform.changeEditor('title2', 'typeId', {
						required: true
					});
					_that.$refs.autoform.changeEditor('title2', 'visitTargetIdStr', {
						required: true
					});
					_that.$refs.autoform.changeEditor('title2', 'visitPreparationIdStr', {
						required: true
					});
				}else{
					_that.$refs.autoform.changeEditor('title2', 'typeId', {
						required: false
					});
					_that.$refs.autoform.changeEditor('title2', 'visitTargetIdStr', {
						required: false
					});
					_that.$refs.autoform.changeEditor('title2', 'visitPreparationIdStr', {
						required: false
					});
				}
			},
			clearContactType(){
				let _that = this;
				_that.$refs.autoform.clear('title2', 'contactTypeId');
				_that.$refs.autoform.changeEditor('title2', 'typeId', {
					required: false
				});
				_that.$refs.autoform.changeEditor('title2', 'visitTargetIdStr', {
					required: false
				});
				_that.$refs.autoform.changeEditor('title2', 'visitPreparationIdStr', {
					required: false
				});
			},
			onSelectVisitPostId(item){
				let _that = this;
				_that.$refs.autoform.formdata.visitPostId = item.id
				_that.$refs.autoform.formdata.visitPostName = item.postName
				_that.$refs.autoform.formdata.visitInstId = item.instId
				_that.$refs.autoform.formdata.visitInstName = item.instName
			},
			clearVisitPostId(){
				let _that = this;
				_that.$refs.autoform.clear('title2', 'visitPostId');
				_that.$refs.autoform.clear('title2', 'visitInstId');
			},
			deleteUserItem(){
				let _that = this
				let peopleIds = []
				if(_that.$refs.autoform.formdata.peopleId){
					peopleIds = _that.$refs.autoform.formdata.peopleId.split(",")
				}
				_that.$refs.autoform.changeEditor('title2', 'peopleId', {
					linkParam: {
						namefield: urlUtil.userPageUrl().namefield,
						searchfield: "q",
						cachename: _that.peopleCacheKey,
						placeholder: urlUtil.userPageUrl().prompt,
						pagination: true,
						hasNum:peopleIds.length
					}
				});
			},
			getInformationByUserId(userId){
				let _that = this;
				_that.om.request(
					"orgInstPosUserOfficial/getUserSalaryPosInfo",
					{ 
						userId:userId
					}
				).then(res=>{
					if(res && res.value){
						let obj = res.value;
						let formdata = _that.$refs.autoform.formdata
						formdata.visitPostId = obj.posId 
						formdata.visitPostName = obj.posName
						formdata.visitInstId = obj.instId
						formdata.visitInstName = obj.instName
					}
				})
			},
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key:_that.customerCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.customerNameId = d.id
							formdata.customerCode = d.customerCode
							formdata.customerName = d.customerName
							formdata.customerShortName = d.customerShortName
							formdata.customerId = d.customerId
							formdata.transactiontatusId = d.tradeStateId
							formdata.transactiontatus = d.tradeStateName
							formdata.problemRemark =  d.problemRemark
							formdata.developmentDate = d.developmentDate
							formdata.orderDate =  d.orderDate
							if(!formdata.visitUserId){
								formdata.visitUserId = _that.customerOwnerId
								formdata.visitUserName = _that.customerOwner
								_that.getInformationByUserId(_that.customerOwnerId)
								_that.om.request('orgInstPosUserOfficial/getPosComboboxByUser', {
									userId: _that.customerOwnerId,
									flag: true,
								}).then(zres => {
									_that.$refs.autoform.formdata.visitPostId = zres[0].id || ''
									_that.$refs.autoform.formdata.visitPostName = zres[0].postName || ''
									_that.$refs.autoform.formdata.visitInstId = zres[0].instId || ''
									_that.$refs.autoform.formdata.visitInstName = zres[0].instName || ''
									_that.$refs.autoform.changeEditor('title2', 'visitPostId', 'data', configSelectData(zres, 'id', 'posname'));
									_that.$refs.autoform.initSelectIndex('title2',"visitPostId");
									// if(zres.length == 1){
									// 	_that.$refs.autoform.initSelectVal('title2', 'visitPostId')
									// }
									uni.removeStorage({
										key:_that.visitUserCacheKey
									})
								})
							}
							uni.removeStorage({
								key:_that.customerCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key: _that.peopleCacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data;
							var formdata = _that.$refs.autoform.formdata;
							if(d.length){
								let idStr = []
								let nameStr = []
								let peopleId = ''
								if(formdata.peopleId){
									peopleId = formdata.peopleId.split(',')
								}
								for(let index in d){
									if(peopleId.indexOf(d[index].id+'') == -1){
										 idStr.push(d[index].id)
										 nameStr.push(d[index].username)
									}
								}
								idStr.map(e =>e).join(",")
								nameStr.map(e =>e).join(",");
								if(!formdata.peopleId){
									formdata.peopleId += idStr
									formdata.people += nameStr
								}else{
									if(idStr.length){
										formdata.peopleId += ','+idStr
										formdata.people +=','+ nameStr
									}
								}
							}
							uni.removeStorage({
								key: _that.peopleCacheKey
							});
							let peopleList = []
							let peopleIds =_that.$refs.autoform.formdata.peopleId.split(',')
							let people = _that.$refs.autoform.formdata.people.split(',')
							_that.$refs.autoform.changeEditor('title2', 'peopleId',{
								linkParam:{
									namefield: urlUtil.userPageUrl().namefield,
									searchfield: "q",
									cachename: _that.peopleCacheKey,
									placeholder: urlUtil.userPageUrl().prompt,
									pagination: true,
									hasNum:peopleIds.length
								}
							})
						}
					}
				});
				uni.getStorage({
					key:_that.typeCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.typeId = d.map(e=>e.id).join(',')
							formdata.type = d.map(e=>e.text).join(',')
							_that.$refs.autoform.changeEditor('title2', 'typeId', {
								initSelect: d
							})
							uni.removeStorage({
								key:_that.typeCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.visitUserCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data[0]
							let formdata = _that.$refs.autoform.formdata
							formdata.visitUserId = d.id
							formdata.visitUserName = d.username
							_that.om.request('orgInstPosUserOfficial/getPosComboboxByUser', {
								userId: d.id,
								flag: true,
							}).then(zres => {
								_that.$refs.autoform.formdata.visitPostId = zres[0].id || ''
								_that.$refs.autoform.formdata.visitPostName = zres[0].postName || ''
								_that.$refs.autoform.formdata.visitInstId = zres[0].instId || ''
								_that.$refs.autoform.formdata.visitInstName = zres[0].instName || ''
								_that.$refs.autoform.changeEditor('title2', 'visitPostId', 'data', configSelectData(zres, 'id', 'posname'));
								_that.$refs.autoform.initSelectIndex('title2',"visitPostId");
								// if(zres.length == 1){
								// 	_that.$refs.autoform.initSelectVal('title2', 'visitPostId')
								// }
								uni.removeStorage({
									key:_that.visitUserCacheKey
								})
							})
						}
					}
				})
				uni.getStorage({
					key:_that.visitTargetIdStrCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.visitTargetIdStr = d.map(e=>e.id).join(',')
							formdata.visitTargetNameStr = d.map(e=>e.text).join(',')
							_that.$refs.autoform.changeEditor('title2', 'visitTargetIdStr', {
								initSelect: d
							})
							uni.removeStorage({
								key:_that.visitTargetIdStrCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.visitPreparationIdStrCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.visitPreparationIdStr = d.map(e=>e.id).join(',')
							formdata.visitPreparationNameStr = d.map(e=>e.text).join(',')
							_that.$refs.autoform.changeEditor('title2', 'visitPreparationIdStr', {
								initSelect: d
							})
							uni.removeStorage({
								key:_that.visitPreparationIdStrCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.exchangePointIdStrCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.exchangePointIdStr = d.map(e=>e.id).join(',')
							formdata.exchangePointNameStr = d.map(e=>e.text).join(',')
							_that.$refs.autoform.changeEditor('title2', 'exchangePointIdStr', {
								initSelect: d
							})
							uni.removeStorage({
								key:_that.exchangePointIdStrCacheKey
							})
						}
					}
				})
			}
		},
		onLoad(e) {
			let _that = this
			subOnLoad(_that, e, function() {
			_that.customerOwnerId = e.customerOwnerId
			_that.customerOwner = e.customerOwner
			if(e.configDataCacheKey){
				uni.getStorage({
					key: e.configDataCacheKey,
					success: function(res) {
						if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
							_that.autoformdata = res.data
							Object.assign(_that.initValue, _that.formdata)
							_that.autoformdata[0].order = _that.order
								changeEditor("title2","customerNameId",{
									"link":"../../public/pageSearch/pageSearch",
									"linkParam":{
										other:JSON.stringify({
											userId:e.customerOwnerId
										}),
										url:"visitPlan/getCustomerName",
										cachename:_that.customerCacheKey,
										initload:true,
										searchfield: 'q',
										namefield:'customerName',
										notefield:'',
									}
								},null,_that.autoformdata)
								changeEditor("title2","typeId",{
									"linkParam":{
										other:JSON.stringify({
											code:'39081'
										}),
										url:"tsysdict/getComboTree",
										cachename:_that.typeCacheKey,
										initload:true,
										namefield:'text'
									}
								},null,_that.autoformdata)
								changeEditor("title2","visitTargetIdStr",{
									"linkParam":{
										other:JSON.stringify({
											code:'39383'
										}),
										url:"tsysdict/getComboTree",
										cachename:_that.visitTargetIdStrCacheKey,
										initload:true,
										namefield:'text'
									}
								},null,_that.autoformdata)
								changeEditor("title2","visitPreparationIdStr",{
									"linkParam":{
										other:JSON.stringify({
											code:'39384'
										}),
										url:"tsysdict/getComboTree",
										cachename:_that.visitPreparationIdStrCacheKey,
										initload:true,
										namefield:'text'
									}
								},null,_that.autoformdata)
								changeEditor("title2","exchangePointIdStr",{
									"linkParam":{
										other:JSON.stringify({
											code:'39701'
										}),
										url:"tsysdict/getComboTree",
										cachename:_that.exchangePointIdStrCacheKey,
										initload:true,
										namefield:'text'
									}
								},null,_that.autoformdata)
								changeEditor('title2',"visitUserId","isSearchUser",true,_that.autoformdata)
								changeEditor('title2',"visitUserId",{
									link:'../../public/searchUser/searchUser',
									"linkParam":{
										cachename: _that.visitUserCacheKey,
										isSingle:true
									}
								},null,_that.autoformdata)
								changeEditor('title2', 'peopleId','isSearchUser', true,  _that.autoformdata);
								changeEditor('title2', 'peopleId', {
									'link':'../../public/searchUser/searchUser',
									linkParam: {
										namefield: urlUtil.userPageUrl().namefield,
										searchfield: "q",
										cachename: _that.peopleCacheKey,
										placeholder: urlUtil.userPageUrl().prompt,
										pagination: true
									}
								}, null, _that.autoformdata);

								_that.$nextTick(function(){
									_that.om.request("tsysdict/getCombobox",{code:'39424'}).then(res=>{
										_that.$refs.autoform.changeEditor('title2',"contactTypeId","data",configSelectData(res,"id","dicname"))
										_that.$refs.autoform.initSelectIndex('title2',"contactTypeId");
									})
									_that.om.request("dictZero/getListByCode",{code:'38586'}).then(res=>{
										_that.$refs.autoform.changeEditor('title2',"timeRangeId","data",configSelectData(res,"id","dicname"))
										_that.$refs.autoform.initSelectIndex('title2',"timeRangeId");
									})
									if(_that.$refs.autoform.formdata.visitPostId){
										_that.om.request('orgInstPosUserOfficial/getPosComboboxByUser', {
											userId: e.customerOwnerId,
											flag: true,
										}).then(zres => {
											_that.$refs.autoform.changeEditor('title2', 'visitPostId', 'data', configSelectData(zres, 'id', 'posname'));
											_that.$refs.autoform.initSelectIndex('title2',"visitPostId");
										})
									}
									
									let peopleIds = []
									if(_that.$refs.autoform.formdata.peopleId){
										peopleIds = _that.$refs.autoform.formdata.peopleId.split(",")
									}
									_that.$refs.autoform.changeEditor('title2', 'peopleId', {
										linkParam: {
											namefield: urlUtil.userPageUrl().namefield,
											searchfield: "q",
											cachename: _that.peopleCacheKey,
											placeholder: urlUtil.userPageUrl().prompt,
											pagination: true,
											hasNum:peopleIds.length
										}
									});
									if(_that.$refs.autoform.formdata.typeId){
										let objArr = []
										let ids = _that.$refs.autoform.formdata.typeId.split(",")
										let names = _that.$refs.autoform.formdata.type.split(",")
										for(let index in ids){
											let obj = {
												id:ids[index],
												dicname:names[index]
											}
											objArr.push(obj)
											_that.$refs.autoform.changeEditor('title2', 'typeId', {
												initSelect: objArr
											})
										}
									}
									if(_that.$refs.autoform.formdata.visitTargetIdStr){
										let objArr = []
										let ids = _that.$refs.autoform.formdata.visitTargetIdStr.split(",")
										let names = _that.$refs.autoform.formdata.visitTargetNameStr.split(",")
										for(let index in ids){
											let obj = {
												id:ids[index],
												dicname:names[index]
											}
											objArr.push(obj)
											_that.$refs.autoform.changeEditor('title2', 'visitTargetIdStr', {
												initSelect: objArr
											})
										}
									}
									if(_that.$refs.autoform.formdata.visitPreparationIdStr){
										let objArr = []
										let ids = _that.$refs.autoform.formdata.visitPreparationIdStr.split(",")
										let names = _that.$refs.autoform.formdata.visitPreparationNameStr.split(",")
										for(let index in ids){
											let obj = {
												id:ids[index],
												dicname:names[index]
											}
											objArr.push(obj)
											_that.$refs.autoform.changeEditor('title2', 'visitPreparationIdStr', {
												initSelect: objArr
											})
										}
									}
								})
							}
						}
					})
				}
			})
		}
	}
</script>

<style>
</style>                                                                                                                                                                                                                                                                                                                                                                                                                                                         
